﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Transactions;

namespace EFTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string connStr = "Server=10.1.81.182;Database=DP;User Id=rod;Password=zzwaust;";

            var target = default(Re);
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("select ID,Name,AddTime,Dataversion from re where id=2", conn);
                var reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    target = new Re()
                    {
                        ID = Convert.ToInt32(reader[0]),
                        Name = Convert.ToString(reader[1]),
                        AddTime = Convert.ToDateTime(reader[2]),
                        Dataversion = (byte[])reader[3]
                    };
                }
            }

            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("update re set addTime=@AddTime where ID=@ID AND Dataversion=@Dataversion", conn);
                SqlParameter Dataversion = new SqlParameter("@Dataversion", SqlDbType.Binary);
                Dataversion.Value = target.Dataversion;
                SqlParameter AddTime = new SqlParameter("@AddTime", SqlDbType.DateTime);
                AddTime.Value = DateTime.Now;
                SqlParameter ID = new SqlParameter("@ID", SqlDbType.Int);
                ID.Value = target.ID;

                cmd.Parameters.Add(AddTime);
                cmd.Parameters.Add(ID);
                cmd.Parameters.Add(Dataversion);
                int result = cmd.ExecuteNonQuery();
                if(result==0)
                {
                    throw new DBConcurrencyException();
                }
            }

           


            using (var context = new DBContextBase(connStr))
            {
                var set = context.Set<Re>();
                var item = set.Where(x => x.ID == 2).FirstOrDefault();
                item.AddTime = DateTime.Now;
                //context.SaveChanges();
            }
            //Console.WriteLine("==============");
            //ThreadLocal<JobInfo> local = null;
            //using (var context = new DBContextBase(connStr))
            //{
            //    var set = context.Set<JobInfo>();
            //    Func<JobInfo, bool> func = x => { return x.EmpId > 12; };
            //    //item = set.Where(x => x.ID == 1).FirstOrDefault();
            //    local = new ThreadLocal<JobInfo>(() => set.Where(func).FirstOrDefault());
            //}

            //using (var context = new DBContextBase(connStr))
            //{
            //    for (int k = currentFlag; k < 1000; k++)
            //    {
            //        var bitarr = new BitArray(flag);
            //        for (int i = 0; i < flag; i++)
            //        {
            //            bitarr[i] = false;
            //        }
            //        bitarr[k] = true;
            //        var buffer = new byte[1000];
            //        bitarr.CopyTo(buffer, 0);


            //        var set = context.Set<BinaryTest>();
            //        var obj = new BinaryTest()
            //        {
            //            Name = "Menu" + k,
            //            Code = buffer
            //        };
            //        set.Add(obj);


            //    }
            //    context.SaveChanges();
            //}



            Console.ReadKey();
        }

        private static void Update(string connStr)
        {
            using (var context2 = new DBContextBase(connStr))
            {
                var _set = context2.Set<Re>();
                var _item = _set.Where(y => y.ID == 2).FirstOrDefault();
                _item.AddTime = DateTime.Now; ;
                _set.Attach(_item);
                context2.Entry(_item).State = EntityState.Modified;
                Thread.Sleep(2000);
                context2.SaveChanges();
            }
        }
    }
}
